package com.rgs.se.concurrent.blockingQueue;

import java.util.Objects;
import java.util.concurrent.BlockingQueue;

/**
 * @author Ren Gaoshuai
 * @ClassName Consumer
 * @Description TODO
 * @date 2021-09-01 22:40
 **/
public class Consumer implements Runnable{
    private BlockingQueue<String> queue;

    public Consumer(BlockingQueue<String> queue){
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("启动消费者线程。");
        boolean isRunning = true;
        try{
            while (isRunning){
                //出队
                String data = queue.poll();
                if (Objects.nonNull(data)){
                    System.out.println("正在消费数据：" + data);
                }else{
                    isRunning = false;
                }
            }
        }finally {
            System.out.println("退出消费者线程。");
        }
    }
}
